#include "stm32f10x.h"
#include "DHT11.h"
#include "OLED.h"
#include "Delay.h"
#include "time.h"
#include "usart.h"
#include "onenet.h"
#include "esp8266.h"
#include "usart_Debug.h"
#include "LED.h"
#include "stdio.h"
#include "Xdelay.h"
#include "PWM.h"
#include "stdbool.h"
#include "sunlight.h"
#include "key.h"
uint8_t humility=0,humilityfloat=0,temperature=0,temperaturefloat=0;

uint16_t timeCount=0;
bool LED_status=0;
bool door_status=0;
uint16_t adcvalue=0;

int main(void)
{
	usart_debug_init();
	PWM_INIT();
//	
	LED_init();
	adc_init();
	key_init();
	unsigned char *dataPtr = NULL;	
	//Servo_SetAngle(0);
	OLED_init();
	DHT11_init();
		OLED_showchinese(1,3,"啊杰家居");
	//printf("6666666666666666\r\n");
	//TIME_init();
	
	OLED_showchinese(2,3,"联网中");

	usart_init();
	
	Delay_Init();
	//OLED_ShowString(4,1,"%RH");
	//OLED_ShowChar(4,1,'@');
	ESP8266_Init();
	//LED_OFF();
	
	//while(ESP8266_SendCmd("AT\r\n", "OK"))
	//while(ESP8266_SendCmd(ESP8266_ONENET_INFO, "CONNECT"))
	//	DelayXms(500);
		//printf("success\r\n");

	while(OneNet_DevLink())			
	DelayXms(500);	
	printf("succeed\r\n");
	//printf("connect MQTT server\r\n");
	OneNET_Subscribe();
	Servo_SetAngle(0);
	door_status=0;
	OLED_clear();
	OLED_showchinese(1,3,"啊杰家居");
	OLED_ShowString(2,5,"%RH");
	OLED_showchinese(3,3,"摄氏度");
	OLED_ShowString(4,5,"Lux");
	OLED_ShowChar(2,3,'.');
	OLED_ShowChar(3,3,'.');
	while(1)
	{
				if(timeCount++>500)
				{
					adcvalue=GetsunlightValue();
					DHT11_Getdata(&humility,&humilityfloat,&temperature,&temperaturefloat);
					OLED_ShowNum(2,1,humility,2);
					OLED_ShowNum(2,4,humilityfloat,1);
					OLED_ShowNum(3,1,temperature,2);
					OLED_ShowNum(3,4,temperaturefloat,1);
					OLED_ShowNum(4,1,adcvalue,4);
				
					
					//发送云平台
					OneNet_SendData();
					timeCount=0;
					ESP8266_Clear();	
					
			}

		dataPtr = ESP8266_GetIPD(0);
		if(dataPtr != NULL)
		{
		//	printf("avcbsggshshss\r\n");
		OneNet_RevPro(dataPtr);
		}
		//delay_us(50);	
		
	}
}

